/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package DAO;

import bean.Idioma;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author jorge
 */
public class IdiomaDao implements Serializable {

    private Conexao conexao;
    private PreparedStatement ps;
    private boolean sucess = false;

    public IdiomaDao() {
        this.conexao = new ConexaoPostgres();

    }

    public boolean cadastrarIdioma(Idioma idioma) {
        sucess = false;
        try {
            System.out.println("IdiomaDAO - Cadastro Idioma: " + idioma.getIdioma());
            ps = conexao.getConnection().prepareStatement("INSERT INTO IDIOMA(idioma) VALUES (?) ");
            ps.setString(1, idioma.getIdioma());

            ps.executeUpdate();
            conexao.closeConnection();

            sucess = true;
        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }
        conexao.closeConnection();
        return sucess;

    }

    public boolean alterarIdioma(Idioma idioma) {
        sucess = false;


        try {

            ps = conexao.getConnection().prepareStatement("UPDATE IDIOMA SET idioma = ? where id = ?");

            ps.setString(1, idioma.getIdioma());
            ps.setInt(2, idioma.getId());
            ps.executeUpdate();
            conexao.closeConnection();

            sucess = true;

        } catch (SQLException ex) {
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
            conexao.closeConnection();
            return sucess;
        }
        conexao.closeConnection();
        return sucess;
    }

    public List<Idioma> listarIdioma() {
        List<Idioma> idiomas = new ArrayList<Idioma>();
        try {
            ps = conexao.getConnection().prepareStatement("SELECT * FROM IDIOMA ORDER BY idioma");
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();

            while (rs.next()) {
                Idioma i = new Idioma();

                i.setIdioma(rs.getString("idioma"));
                i.setId(rs.getInt("id"));

                idiomas.add(i);


            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            Logger.getLogger(InstituicaoDao.class.getName()).log(Level.SEVERE, null, ex);
        }

        conexao.closeConnection();
        return idiomas;

    }

    public Idioma consultarIdioma(int id) {
        PreparedStatement ps = null;
        Idioma i = null;
        try {
            i = new Idioma();
            ps = conexao.getConnection().prepareStatement("Select * from IDIOMA where id = ?; ");
            ps.setInt(1, id);
            ResultSet rs = ps.executeQuery();
            conexao.closeConnection();
            if (rs.next()) {

                i.setIdioma(rs.getString("idioma"));
                i.setId(id);


            }
        } catch (SQLException ex) {
            conexao.closeConnection();
            return i;

        }
        conexao.closeConnection();
        return i;
    }

    public boolean removerIdioma(Idioma idioma) {
        sucess = false;
        try {

            ps = conexao.getConnection().prepareStatement(" DELETE FROM IDIOMA WHERE id = ?");

            ps.setLong(1, (idioma.getId()));

            ps.executeUpdate();
            conexao.closeConnection();


            sucess = true;
        } catch (SQLException ex) {
            conexao.closeConnection();
            ex.printStackTrace();
            return sucess;
        }

        conexao.closeConnection();
        return sucess;
    }
}
